package com.example.onlineeducation.widget

import android.app.Dialog
import android.content.Context
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.onlineeducation.R
import com.example.onlineeducation.adapter.AddPictureAdapter
import com.example.onlineeducation.adapter.SignBehalfAddPictureAdapter
import com.example.onlineeducation.data.entity.SignInEntity
import com.example.onlineeducation.utils.YcLoopCountDown
import com.example.onlineeducation.utils.pictureseletor.YcFullyGridLayoutManager
import com.example.onlineeducation.utils.pictureseletor.YcPictureSelecorUtil
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.yc.jetpacklib.extension.ycIsEmpty
import com.yc.jetpacklib.widget.YcBtnCommon
import com.yc.jetpacklib.widget.dialog.YcOnClick
import java.lang.ref.WeakReference

class SignBehalfPictureDialog @JvmOverloads constructor(
    context: FragmentActivity,
    mLifecycleOwner: LifecycleOwner,
    theme: Int = R.style.YcCommonDialogStyle
) :
    Dialog(context, theme) {
    private var rvAddPictures: RecyclerView
    private var mBtnCancel: YcBtnCommon
    private var mBtnOk: YcBtnCommon
    private var mOnBtnCancelClick: YcOnClick? = null
    private var mOnBtnOkClick: YcOnClick? = null
    private var selectList: MutableList<LocalMedia> = mutableListOf()
    private var maxImages = 1
    private val mAdapter by lazy {
        SignBehalfAddPictureAdapter(context, maxImages).apply {
            itemClick = { position, _ ->
                YcPictureSelecorUtil.previewPicture(
                    context, position,
                    selectList
                )
            }
            addPictureClick = {
                YcPictureSelecorUtil.addMultiplePicture(context,
                    maxImages,
                    selectList,
                    object : OnResultCallbackListener<LocalMedia> {
                        override fun onResult(result: MutableList<LocalMedia>?) {
                            if (result != null) {
                                selectList.clear()
                                selectList.addAll(result)
                                notifyDataSetChanged()
                            }
                        }

                        override fun onCancel() {}
                    })
            }
        }
    }


    init {
        mLifecycleOwner.lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onDestroy(owner: LifecycleOwner) {
                if (this@SignBehalfPictureDialog.isShowing) {
                    cancel()
                }
            }
        })
        setContentView(R.layout.offline_study_sign_behalf_picture_dialog)
        rvAddPictures = findViewById<View>(R.id.rvAddPictures) as RecyclerView
        mBtnCancel = findViewById<View>(R.id.btnCancel) as YcBtnCommon
        mBtnOk = findViewById<View>(R.id.btnOk) as YcBtnCommon
        //设置对话框位置大小
        val dialogWindow = window
        dialogWindow!!.setGravity(Gravity.CENTER_HORIZONTAL or Gravity.CENTER_VERTICAL)
        dialogWindow.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT
        )
        val lp = dialogWindow.attributes
        dialogWindow.attributes = lp //此处暂未设置偏移量
        setCancelable(false)
        setCanceledOnTouchOutside(false)

        rvAddPictures.apply {
            layoutManager = YcFullyGridLayoutManager(
                context,
                1,
                GridLayoutManager.VERTICAL,
                false
            )
            adapter = mAdapter
            mAdapter.setList(selectList)
        }
        mBtnCancel.setOnClickListener {
            dismiss()
            mOnBtnCancelClick?.onClick()
        }
        mBtnOk.setOnClickListener {
            mOnBtnOkClick?.onClick()
        }
    }

    fun isEmpty(): String {
        return when (selectList.size) {
            0 -> {
                "请上传签退照片"
            }
            else -> {
                ""
            }
        }
    }

    /**
     * 设置确定按钮点击事件
     */
    fun setOkOnClick(onBtnOkClick: YcOnClick?): SignBehalfPictureDialog {
        mOnBtnOkClick = onBtnOkClick
        return this
    }

    /**
     * 设置取消按钮点击事件
     */
    fun setCancelOnClick(onCancelClick: YcOnClick?): SignBehalfPictureDialog {
        mOnBtnCancelClick = onCancelClick
        return this
    }


    fun getUploadEntity(): MutableList<LocalMedia> {
        return selectList
    }

    fun resetData() {
        selectList.clear()
        mAdapter.notifyDataSetChanged()
    }

    @Override
    override fun dismiss() {
        super.dismiss()
        resetData()
    }
}